// 同过multer上传文件路由
var express = require("express");
var router = express.Router();
const fs = require("fs");
const multer = require("multer");
const path = require("path");
const myCustomStorage = require("../src/utils/MyCustomStorage");
const { pathType,status } = require("../src/config");
const storage = myCustomStorage({
  destination: function (req, file, cb) {
    // 判断是否存在文件夹，不存在则创建
    if (!fs.existsSync("public"+ pathType + 'uploadfile' + pathType + req.body.name)) {
      fs.mkdirSync("public"+ pathType + 'uploadfile' + pathType + req.body.name);
    }
    cb(null, {path:'public'+ pathType + 'uploadfile' + pathType + req.body.name,base:'public'});
  },
  filename: function (req, file, cb) {
    let fileName = Date.now() + "." + file.originalname.split(".")[1]
    if(file.originalname.split(".")[1] == 'keystore'){
      fileName = file.originalname
    }
    cb(null, fileName);
  }
});

const upload = multer({ storage });

router.post("/",upload.single("file"),function (req, res, next) { 
  console.log(req.body,req.file,'req.file')
  // 获取upload返回的信息
  res.send(status({msg:'上传成功',data:req.file}));
});


module.exports = router;
